<script setup>
import { ref } from 'vue'
import { Search } from '@element-plus/icons-vue'
import { Edit } from '@element-plus/icons-vue'
import AccountEdit from './components/AccountEdit.vue'
import { getAccountListService, changeStatusService, resetPwdService } from '@/api/account'
const params = ref({
  pageNo: 1,
  pageSize: 5,
  account: ''
})
const total = ref(0)
const users = ref([
  {
    id: '',
    account: '',
    userName: '',
    createDate: '',
    enabled: '',
    roleName: ''
  }
])
const loading = ref(false)
const getUserList = async () => {
  loading.value = true
  const res = await getAccountListService(params.value)
  users.value = res.data.data
  total.value = res.data.total
  loading.value = false
}
getUserList()
const on = async (id, status) => {
  console.log(id, status)
  await changeStatusService(id, status)
  ElMessage({ type: 'success', message: '更新成功' })
  getUserList()
}
const off = async (id, status) => {
  await changeStatusService(id, status)
  ElMessage({ type: 'success', message: '更新成功' })
  getUserList()
}
const onSizeChange = (size) => {
  params.value.pageNo = 1
  params.value.pageSize = size
  getUserList()
}
const onCurrentChange = (page) => {
  params.value.pageNo = page
  getUserList()
}
const reset = async (id) => {
  await resetPwdService(id)
  ElMessage({ type: 'success', message: '重置密码成功 密码为123' })
}
//配置状态表格渲染规则
const tableRowClassName = ({ row }) => {
  if (row.enabled === 0) {
    return 'warning-row'
  } else {
    return 'success-row'
  }
}
const dialog = ref()
//修改用户账户信息
const onEditAccount = async (row) => {
  console.log(row)
  dialog.value.open(row)
}
const onSuccess = async () => {
  getUserList()
}
</script>

<template>
  <page-container title="账号管理">
    <template #extra>
      <el-input placeholder="请输入账号" v-model="params.account" clearable @clear="getUserList">
        <template #append>
          <el-button @click="getUserList"
            ><el-icon><search /></el-icon
          ></el-button>
        </template>
      </el-input>
    </template>
    <el-table
      :data="users"
      border
      style="width: 100%"
      v-loading="loading"
      :row-class-name="tableRowClassName"
    >
      <el-table-column prop="createDate" label="注册日期" width="105" />
      <el-table-column prop="account" label="用户账号" />
      <el-table-column prop="userName" label="用户昵称" />
      <el-table-column prop="roleName" label="用户角色" />
      <el-table-column fixed="right" label="操作" width="245">
        <template #default="{ row }">
          <el-button :icon="Edit" plain type="primary" @click="onEditAccount(row)"></el-button>
          <el-button type="success" @click="on(row.id, 1)" v-if="row.enabled === 0">启用</el-button>
          <el-button type="danger" @click="off(row.id, 0)" v-else>禁用</el-button>
          <el-button type="primary" @click="reset(row.id)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="params.pageNo"
      v-model:page-size="params.pageSize"
      :page-size="[2, 3, 5, 10]"
      layout="jumper, total, sizes, prev, pager, next"
      background
      :total="total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
      style="margin-top: 20px; justify-content: flex-end"
    >
    </el-pagination>
    <account-edit ref="dialog" @success="onSuccess"></account-edit>
  </page-container>
</template>

<style>
.el-table .warning-row {
  --el-table-tr-bg-color: #5d291e;
}
.el-button--danger {
  --el-button-bg-color: #e97878;
}
.el-button--primary {
  --el-button-bg-color: #69a9ec;
}
.el-table .success-row {
  --el-table-tr-bg-color: #214d09;
}
</style>
